package net.j4love.spring.mvc.ex.validation;

import net.j4love.spring.mvc.ex.model.User;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;

/**
 * @author He Peng
 * @create 2017-06-29 16:54
 * @update 2017-06-29 16:54
 * @see
 */

@Controller
@RequestMapping(
        path = "/validation"
)
public class ArgValidationController {

    // 很不幸的是 spring mvc 是不支持这种方式的参数校验的
    @RequestMapping(
            path = "/simple"
    )
    public ModelAndView simpleValidHandler(@Valid @NotBlank String str) {
        ModelAndView modelAndView = new ModelAndView("argValidation");
        modelAndView.addObject("path","/simple");
        modelAndView.addObject("msg", str);
        return modelAndView;
    }

    @RequestMapping(
            path = "/pojo"
    )
    public ModelAndView pojoValidHandler(@Valid User user) {
        ModelAndView modelAndView = new ModelAndView("argValidation");
        modelAndView.addObject("path","/pojo");
        modelAndView.addObject("msg", user);
        return modelAndView;
    }


    @RequestMapping(
            path = "/pojo/complex"
    )
    public ModelAndView complexPojoValidHandler(@Valid User user) {
        ModelAndView modelAndView = new ModelAndView("argValidation");
        modelAndView.addObject("path","/pojo/complex");
        modelAndView.addObject("msg", user);
        return modelAndView;
    }
}
